package top.jlzhou.tools.office;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

import top.jlzhou.tools.office.excel.Excel2007;
import top.jlzhou.tools.office.excel.ExcelXP;


/**
 * <pre>Excel操作工具类</pre>
 * @ClassName	top.jlzhou.tools.office.Excel
 * @author	J.L.Zhou
 * @date	2019年3月9日 下午6:55:53
 * @version 	V1.0
 * @Copyright	2019 jlzhou.top Inc. All rights reserved. 
 * 	注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目
 */
public abstract class Excel {
	
	public static Excel open(File file)throws Exception{
		return open(new FileInputStream(file));
	}
	
	public static Excel open(InputStream inputStream)throws Exception{
		try{
			return new Excel2007(inputStream);
		}catch(Exception ex){
			return new ExcelXP(inputStream);
		}
	}
	
	
	public Row getRow(int sheetIndex, int rowIndex){
		Row row = null;
		try{
			row = getWorkbook().getSheetAt(sheetIndex).getRow(rowIndex);
		}catch(Exception ex){
			row = getWorkbook().getSheetAt(sheetIndex).createRow(rowIndex);
		}
		if(row==null){
			row = getWorkbook().getSheetAt(sheetIndex).createRow(rowIndex);
		}
		return row;
	}
	public Cell getCell(int sheetIndex, int rowIndex, int colIndex){
		
		Cell cell= null;
		try{
			cell = getRow(sheetIndex, rowIndex).getCell(colIndex);
		}catch(Exception ex){
			cell = getRow(sheetIndex, rowIndex).createCell(colIndex);
		}
		if(cell == null){
			cell = getRow(sheetIndex, rowIndex).createCell(colIndex);
		}
		return cell;
	}

	public abstract Workbook getWorkbook();
	
	/**
	 * 获取电子表格的工作表名数组
	 * @return
	 */
	public abstract String[] listSheet();
	

	/**
	 * 获取第sheetIndex张电子表格的全部数据
	 * @param sheetIndex
	 * @return
	 */
	public  List<List<String>> read(int sheetIndex){
		return read(sheetIndex, 0);
	}
	
	/**
	 * 获取第sheetIndex张电子表格从startRow行开始的全部数据
	 * @param sheetIndex
	 * @param startRow
	 * @return
	 */
	public  List<List<String>> read(int sheetIndex,int startRow){
		return read(sheetIndex, startRow, Integer.MAX_VALUE);
	}
	
	/**
	 * 获取第sheetIndex张电子表格从startRow行到endRow的的全部数据
	 * @param sheetIndex
	 * @param startRow
	 * @param endRow
	 * @return
	 */
	public abstract List<List<String>> read(int sheetIndex,int startRow,int endRow);
	
	/**
	 * 
	 * @param sheetIndex
	 * @param r
	 * @param c
	 * @return
	 */
	public abstract String readOne(int sheetIndex,int r,int c);
	
	/**
	 * 
	 * @param sheetIndex
	 * @param rowIndex
	 * @param colIndex
	 * @param value
	 */
	public abstract void write(int sheetIndex,int rowIndex,int colIndex,boolean value)throws Exception;
	public abstract void write(int sheetIndex,int rowIndex,int colIndex,Date value)throws Exception;
	public abstract void write(int sheetIndex,int rowIndex,int colIndex,String value)throws Exception;
	public abstract void write(int sheetIndex,int rowIndex,int colIndex,double value)throws Exception;
	
	/**
	 * 刷新第sheetIndex张电子表格的公式结果
	 * @param sheetIndex
	 */
	public abstract void reload(int sheetIndex);
	
	public abstract void save(File file) throws Exception;
	
	public abstract void save(OutputStream out) throws Exception;
	
	
}
